package edu.hitsz.application;

import edu.hitsz.scoreDAO.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Objects;

public class RankScreen {
    private JPanel MainPanel;
    private JPanel midPanel;
    private JPanel bottomPanel;
    private JPanel topPanel;
    private JLabel modeLabel;
    private JTable scoreTable;
    private JScrollPane tableScrollPanel;
    private JButton deleteButton;
    private JLabel modeValue;


    public JPanel getMainPanel() {
        return MainPanel;
    }

    private DefaultTableModel model;

    public RankScreen() {

        loadScoreTable();

        setGameModeLabel();

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row=scoreTable.getSelectedRow();
                System.out.println("delete row:"+row);
                int result = JOptionPane.showConfirmDialog(deleteButton,
                        "是否确定中删除？");
                if (JOptionPane.YES_OPTION == result && row != -1) {
                    model.removeRow(row);
                    //删除排名为row+1的玩家信息
                    try {
                        ScoreDao scoreDao=new ScoreDaoFile();
                        scoreDao.doDelete(row+1);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }

                    //更新model
                    loadScoreTable();
                }
            }
        });
    }

    public void setGameModeLabel(){
        if(Main.gameMode==Main.EASY_MODE){
            modeValue.setText("EASY");
        }else if (Main.gameMode==Main.NORMAL_MODE){
            modeValue.setText("NORMAL");
        }else if(Main.gameMode==Main.HARD_MODE){
            modeValue.setText("HARD");
        }

    }


    public void add(String name,int score){
        //存储数据
        ScoreDao scoreDao;
        try {
            scoreDao=new ScoreDaoFile();
            if(name!=null){
                if (Objects.equals(name, "")){
                    name="不知名的高手";
                }
                scoreDao.doAdd(name,score);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //控制台打印数据
        try {
            System.out.println("*********************************");
            System.out.println("           得分排行榜");
            System.out.println("*********************************");
            LinkedList<UserInfo> allUsers= scoreDao.getAllUsers();
            for(UserInfo user :allUsers){
                System.out.println("第"+user.getRank()+"名:"+user.getUserName()+","+user.getScore()+","+user.getTime());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        loadScoreTable();
    }


    /**
     * 加载ScoreTableModel
     * @return
     */
    public DefaultTableModel loadModel(){
        LinkedList<UserInfo> allUsers;
        try {
            ScoreDao scoreDao=new ScoreDaoFile();
            allUsers= scoreDao.getAllUsers();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        int row=allUsers.size();
        int col=4;

        String[] columnName = {"名次","玩家名","得分","记录时间"};
        String[][]tableData=new String[row][col];

        int p=0;
        for (UserInfo user:allUsers){
            tableData[p][0]=Integer.toString(user.getRank());
            tableData[p][1]=user.getUserName();
            tableData[p][2]=Integer.toString(user.getScore());
            tableData[p][3]=user.getTime();
            p+=1;
        }

        //表格模型
        DefaultTableModel model = new DefaultTableModel(tableData, columnName){
            @Override
            public boolean isCellEditable(int row, int col){    //重写方法为不可编辑表格内容
                return false;
            }
        };
        return model;
    }

    public void loadScoreTable(){
        //表格模型
        model=loadModel();
        //JTable并不存储自己的数据，而是从表格模型那里获取它的数据
        scoreTable.setModel(model);
        tableScrollPanel.setViewportView(scoreTable);
    }
}
